<?php
/**
 * alltosun.com  FamiliesController.Class.php
 * ============================================================================
 * 版权所有 (C) 2015 齐齐哈尔大学计算机与控制工程学院，并保留所有权利。
 * ============================================================================
 * $Author: 范志俊 (fanfzj@163.com) $
 * $Date: 2015-10-22 上午12:24:54 $
 */
namespace Admin\Controller;
use Think\Controller;

class MajorController extends AuthController {

    public function index()
    {
        $Major = M('Major');

        $total = $Major->count();
        if( $total ){
            $perNum=25;
            $Page=new \Think\Page( $total , $perNum );

            $Page->setConfig( 'prev' , "上一页" );//上一页
            $Page->setConfig( 'next' , '下一页' );//下一页
            $Page->setConfig( 'first' , '首页' );//第一页
            $Page->setConfig( 'last' , "末页" );//最后一页
            $Page->setConfig( 'theme' , '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%' );

            $show=$Page->show();

            $this->assign( 'total' , $total );
            $this->assign( 'page' , $show );

        }

        $major_list = $Major->order('Id asc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('major_list', $major_list);

        $this->display('major_list');
    }

    //添加科
    public function add()
    {
        $this->display('major_add');
    }

    //编辑科
    public function edit()
    {
        $id = I('id');
        $major_info = M('Major')->where(array('Id' => $id))->find();
        $this->assign('major_info', $major_info);
        $this->display('major_add');
    }

    //保存数据
    public function save()
    {
        $id = I('id');
        $info = M('Major')->where(array('Id' => $id))->find();

        $data['name']  = I('name', '');

        if($info){
            //更新操作
            $result = M('Major')->where(array('Id' => $id))->save($data);
            if($result || $result === 0){
                $this->ajaxReturn(array('status' => 'ok', 'info' => '专业修改成功,正在转跳到专业列表'));
            }else if($result === FALSE){
                $this->ajaxReturn(array('status' => 'error', 'info' => '专业修改失败'));
            }
        }else{
        	//入库操作
            $data['add_time'] = date('Y-m-d H:i:s', time());
            $result = M('Major')->add($data);
            if($result){
                $this->ajaxReturn(array('status' => 'ok', 'info' => '专业添加成功,正在转跳到专业列表'));
            }else if($result){
                $this->ajaxReturn(array('status' => 'error', 'info' => '专业添加失败'));
            }
        }

    }

    public function del()
    {
        $id=I( 'Id' );
        $result=M( 'Major' )->delete( $id );
        if($result){
            $data[ 'status' ]=1;
            $data[ 'data' ]='专业删除成功';
        }else if($result){
            $data[ 'status' ]=0;
            $data[ 'data' ]='专业删除失败,请刷新重试';
        }
        $this->ajaxReturn( $data );
    }
    //检测科是否存在
    public function check_title()
    {
        $title = I('name');
        $result = check_title('major', $title);

        if($result){
            $this->ajaxReturn(array('status' => 'n', 'info' => '该专业已经存在'));
        }else{
            $this->ajaxReturn(array('status' => 'y', 'info' => ''));
        }
    }

}
